package com.example.demo.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.bean.SRegion;
import com.example.demo.common.exception.DataValidException;
import com.example.demo.mapper.SCustomerMapper;
import com.example.demo.mapper.SDeptMapper;
import com.example.demo.mapper.SEmpMapper;
import com.example.demo.mapper.SRegionMapper;
import com.example.demo.mapper.SWarehouseMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class SRegionService {
	@Autowired
	private SRegionMapper srMapper;
	@Autowired
	private SCustomerMapper scMapper;
	@Autowired
	private SDeptMapper sdMapper;
	@Autowired
	private SEmpMapper seMapper;
	@Autowired
	private SWarehouseMapper swMapper;
	
	//搜索所有区域信息
	public PageInfo<SRegion> findAllByPage(int pageNum,int pageSize){
		PageHelper.startPage(pageNum,pageSize);
		//执行查找行为
		List<SRegion> list =srMapper.selectAll();
		if (list==null) {
			list=new ArrayList<SRegion>(0);
		}
		return new PageInfo<SRegion>(list);
	}
	
	//按条件搜索区域信息
	public PageInfo<SRegion> findByName(String name, int pageNum, int pageSize){
		PageHelper.startPage(pageNum,pageSize);
		name=(name==null||"".equals(name))?"":"%"+name+"%";
		List<SRegion> list=srMapper.selectByName(name);
		if (list==null) {
			list=new ArrayList<>(0);
			System.out.println("====================================================");
		}
		
		return new PageInfo<>(list);
	}
	
	//按照id查找区域信息
	public SRegion findById(Integer id){
		SRegion sr=srMapper.selectByPrimaryKey(id);
		return sr;
	}
	
	//添加或修改区域信息
	public void saveOrUpdate(SRegion sr) {
		//判断输入的信息是否存在
		if (sr.getId()==null) {
			srMapper.insert(sr);
		}else {
			srMapper.updateByPrimaryKey(sr);
		}
	}
	
	//删除区域信息
	//区域信息作为customer,dept,emp,warehouse外键
	//删除前先判断下属信息是否存在
	//不存在的话删除
	public int deleteById(Integer id) {
		//判断id是否为空
		if (id==null) {
			throw new DataValidException("请输入区域id");
		}
		//按照区域id查找信息，如果找不到就提示删除失败
		SRegion sRegion=srMapper.selectByPrimaryKey(id);
		if (sRegion==null) {
			throw new DataValidException("删除失败");
		}
		if (!scMapper.selectByRegionId(sRegion.getId()).isEmpty()||
			!sdMapper.selectByRegionId(sRegion.getId()).isEmpty()||	
			!swMapper.selectByRegionId(sRegion.getId()).isEmpty()) {
			throw new DataValidException("存在下属信息，客户，部门，员工，仓库，清删除所有下属信息后在进行删除");
		}
		srMapper.deleteByPrimaryKey(id);
		return 0;
	}
	
	//批量删除区域信息
	public int batchDeleteById(int[] id) {
		//判断是否为空
		if (id.length==0||id==null) {
			throw new DataValidException("请选择一批id");
		}
		//查找要删除的顾客信息
		for (int i : id) {
			SRegion sRegion=srMapper.selectByPrimaryKey(i);
			if (sRegion==null) {
				throw new DataValidException("删除失败");
			}
			if (!scMapper.selectByRegionId(sRegion.getId()).isEmpty()||
					!sdMapper.selectByRegionId(sRegion.getId()).isEmpty()||	
					!swMapper.selectByRegionId(sRegion.getId()).isEmpty()) {
					throw new DataValidException("存在下属信息，客户，部门，员工，仓库，清删除所有下属信息后在进行删除");
				}
		}
		//删除区域信息
		srMapper.batchDelete(id);
		return 0;
	}
}
